package com.czh.springboot04.service.serviceimpl;

import com.czh.springboot04.entity.Question;
import com.czh.springboot04.entity.User;
import com.czh.springboot04.entity.Reply;
import com.czh.springboot04.mapper.QuestionMapper;
import com.czh.springboot04.mapper.ReplyMapper;
import com.czh.springboot04.service.QuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.czh.springboot04.mapper.UserMapper;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ReplyMapper replyMapper;
    @Override
    public int addQuestion(Question question) {
        return questionMapper.addQuestion(question);
    }

    @Override
    public int deleteQuestion(int id) {
        return questionMapper.deleteQuestion(id);
    }

    @Override
    public int updateQuestion(Question question) {
        return questionMapper.updateQuestion(question);
    }

    @Override
    public PageInfo<Question> findAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Question> questions=questionMapper.findAll();
        //System.out.println("questions"+questions);
        for (Question question:questions){
            Reply reply=replyMapper.findById(question.getId());
            //System.out.println("reply"+reply);
            question.setReply(reply);
        }
        return new PageInfo<>(questions);
    }

    @Override
    public PageInfo<Question> findByAll(int pageNum, int pageSize) {
        List<Question> questions=questionMapper.findByAll();
        for(Question question:questions){
            User user=userMapper.selectById(question.getUserId());
            question.setUser(user);
        }
        PageInfo<Question> pageInfo=new PageInfo<>(questions);
        return pageInfo;
    }


    @Override
    public List<Question> findById(int id) {
        List<Question> questions = new ArrayList<>();
        Question question;
        int i = 0;
        while (true) {
            //System.out.println(i);
            if(i==0){
                question = questionMapper.findById(id);
            }else {
                if(questions.get(i-1).getReply()==null){
                    break;
                }
                question = questionMapper.findById(questions.get(i-1).getReply().getFid());
            }
            if(question==null){
                break;
            }
            questions.add(question);
            //System.out.println("question"+question);
            Reply reply = replyMapper.findById(questions.get(i).getId());
            //System.out.println("reply"+reply);
            if(reply!=null){
                questions.get(i).setReply(reply);
            }

            //System.out.println(questions.get(i));
            //System.out.println("reply"+reply);
            User user = userMapper.selectById(questions.get(i).getUserId());
            //System.out.println("user"+user);
            if(user!=null){
                questions.get(i).setUser(user);
            }
            i++;
        }
        return questions;
    }
}
